<?php
/*------------------------------------------------------------------------
# SP Comments Editors button
# ------------------------------------------------------------------------
# Author    JoomShaper http://www.joomshaper.com
# Copyright (C) 2010 - 2012 JoomShaper.com. All Rights Reserved.
# @license - http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL
# Websites: http://www.joomshaper.com
-------------------------------------------------------------------------*/

// no direct access
defined( '_JEXEC' ) or die( 'Restricted access' );

jimport( 'joomla.plugin.plugin' );

class plgButtonSPComments extends JPlugin
{

	function plgButtonSPComments(& $subject, $config)
	{
		parent::__construct($subject, $config);
	}

	/**
	 * @return array A two element array of ( imageName, textToInsert )
	 */
	public function onDisplay($name)
	{
		$app = JFactory::getApplication();

		$doc 		=& JFactory::getDocument();
		$template 	= $app->getTemplate();

		// button is not active in specific content components

		$getContent = $this->_subject->getContent($name);
		$present = JText::_('There is already an SP Comments tag in this article', true) ;
		$js = "
			function insertComments(editor) {
				var content = $getContent;
				
				if (content.match(/{spcomments on}/i)) {
					var ask = confirm('SP Comments is currently turned on and it will be turned off now. OK ?');
					
					if (ask == true) {
						var getContent = window.parent.tinyMCE.activeEditor.getContent();
						replacedContent = content.replace(\"{spcomments on}\", \"{spcomments off}\");
						window.parent.tinyMCE.activeEditor.setContent(replacedContent);
					}
				} else if (content.match(/{spcomments off}/i)) {
					var ask = confirm('SP Comments is currently turned off and it will be turned on now. OK ?');
					
					if (ask == true) {
						var getContent = window.parent.tinyMCE.activeEditor.getContent();
						replacedContent = content.replace(\"{spcomments off}\", \"{spcomments on}\");
						window.parent.tinyMCE.activeEditor.setContent(replacedContent);
					}
				} else {
					jInsertEditorText(\"{spcomments on}\", editor);
				}
			}
			";

		$doc->addScriptDeclaration($js);

		$button = new JObject();
		$button->set('modal', false);
		$button->set('onclick', 'insertComments(\''.$name.'\');return false;');
		$button->set('text', JText::_('SP Comments'));
		$button->set('name', 'readmore');
		$button->set('link', '#');

		return $button;
	}
}